{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2023-12-28T07:41:58.938303600Z",
     "start_time": "2023-12-28T07:41:58.756002600Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import re\n",
    "df = pd.read_csv('../static/data/dangdang.csv')\n",
    "df['rank'] = df['rank'].astype(int)\n",
    "df['comment_count'] = df['comment_count'].apply(lambda x:''.join(re.findall('\\d+?',x)))\n",
    "df['comment_count'] = df['comment_count'].apply(lambda x: 0 if x=='' else int(x))\n",
    "df['recommend_percent'] = df['recommend_percent'].apply(lambda x:x[:-3]).astype(float)\n",
    "df['publisher_info'] = df['publisher_info'].apply(lambda x:'' if pd.isnull(x) else x)\n",
    "df['publish_date'] = df['publisher_info'].apply(lambda x:''.join(re.findall('\\d{4}-\\d{2}-\\d{2}',x)))\n",
    "df['publish_year_month'] = df['publish_date'].apply(lambda x:''.join(re.findall('\\d{4}-\\d{2}',x)))\n",
    "df['publish_info_new'] = df['publisher_info'].apply(lambda x:re.sub('\\d{4}-\\d{2}-\\d{2}','|',x))\n",
    "df['publish_house'] = df['publish_info_new'].apply(lambda x:''.join(re.findall('\\|(.*?社)',x)))\n",
    "df['publisher'] = df['publish_info_new'].apply(lambda x:re.sub('\\|.*?社','',x))\n",
    "df['price_n'] = df['price_n'].apply(lambda x:x.split('¥')[1])\n",
    "df['price_n'] = df['price_n'].apply(lambda x:''.join(re.findall('\\d',x))).astype(float)\n",
    "df['price_n'] = df['price_n'].apply(lambda x: x/100)\n",
    "df['price_r'] = df['price_r'].apply(lambda x:x.split('¥')[1])\n",
    "df['price_r'] = df['price_r'].apply(lambda x:''.join(re.findall('\\d',x))).astype(float)\n",
    "df['price_r'] = df['price_r'].apply(lambda x: x/100)\n",
    "df['price_s'] = df['price_s'].apply(lambda x:''.join(re.findall('(.*?)折',x))).astype(float)\n",
    "df_pre = df[['rank','name','comment_count','recommend_percent','price_n','price_r','price_s','publish_date','publish_year_month','publish_house','publisher']]\n",
    "df_pre.to_csv('../static/data/book_info_pre.csv',encoding='utf_8_sig',index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
